package com.aop;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;

public class SessionFilter implements Filter {
	public void init(FilterConfig filterConfig) throws ServletException {	
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		
		HttpServletRequest req=(HttpServletRequest)request;
		String uri=req.getRequestURI();
		System.out.println(uri);
		
		HttpSession session =req.getSession();
		
		String [] excludePaths= {
				"login.jsp",
				"register.jsp",
				"UserServlet",
				"images",
				"js",
				"css"
		};
		
		
		for(String path:excludePaths) {
			if(uri.contains(path)) {
				chain.doFilter(request, response);
				return;
			}	
		}

		if(session.getAttribute("session_user")==null) {
			String script="<script>alert('session timeout!');window.top.location.href='login.jsp'</script>";
			response.getWriter().print(script);
		}
		else {
			chain.doFilter(request, response);
		}					
	}

	public void destroy() {
	}
}
